#!/usr/bin/env python
#coding: utf-8
"""
Management utility for deploy current django project with hg.
"""

import os,sys
import getpass


from django.core.management.base import NoArgsCommand, CommandError
from django.core.cache import cache

from common import settings

class Command(NoArgsCommand):
    
    
    help = 'Used to deploy the current django project static files with hg.'
    
    VERSION_PREFIX = 'release_'
    
    def handle_noargs(self, **options):
        
        sys.stdout.write("Start to deploy the django project  %s static files.\n" %settings.DOMAIN_NAME)
                
        # 检查当前运行的系统用户是否合法的DEPLOY_USER_NAME
#        if getpass.getuser() != settings.DJANGO_USER:
#            raise CommandError("Current system user is not %s." %settings.DJANGO_USER)
#            return
        
        # 输入发布的版本号
#        while True:
#            version = raw_input("Enter deploy version: ")
#            if version:
#                break
#        version = self.VERSION_PREFIX + version
#        
#        # 更新代码
#        sys.stdout.write("Run \"hg pull\" and \"hg update %s -C\" to update the tag %s.\n" %(version, version))
#        status = os.system("hg pull")
#        if status != 0:
#            raise CommandError("Run command \"hg pull\" failed.")
#        
#        status = os.system("hg update %s -C" %version)
#        if status != 0:
#            raise CommandError("Run command \"hg update %s -C\" failed."%version)
        
        #grunt 静态文件
        sys.stdout.write("Run \"python manage.py grunt\" to grunt the static files.\n")
        status = os.system("python manage.py grunt")
        if status != 0:
            raise CommandError("Run command \"python manage.py grunt\" failed.")
        
        # 发布静态文件
        sys.stdout.write("Run \"python manage.py collectstatic --noinput\" to deploy the static files.\n")
        status = os.system("python manage.py collectstatic --noinput")
        if status != 0:
            raise CommandError("Run command \"python manage.py collectstatic --noinput\" failed.")
            
            
        sys.stdout.write("Deploy the version %s of project %s static files successfully.\n" %("",settings.DOMAIN_NAME))
